//index.js
//获取应用实例
const app = getApp()



Page({
  data: {
    userInfo: {
      avatarUrl: "",
      city: "",
      country: "China",
      gender: 1,
      language: "zh_CN",
      nickName: "",
      province: "",
      password: ""
    }
  },
  onLoad: function () {
       this.validateAuth();  
  },

  validateAuth() {
    //查看本地存储的用户，即是否登录过，登录过直接跳转到首页
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },

  bindsubmit(e) {
    let formValue = e.detail.value;
    let {
      nickName,
      password
    } = formValue;
    if (!nickName || !password) {
      wx.showToast({
        title: '账号或密码错误',
        icon: 'none',
      })
      return;
    }
    this.data.userInfo.nickName = nickName
    this.data.userInfo.password = password;
    this.data.userInfo.avatarUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598247547343&di=a210b731340fde5b44bdfb0fee69dfb0&imgtype=0&src=http%3A%2F%2Ftrademark-pics-search.oss-cn-shanghai.aliyuncs.com%2Fsmall%2Fs4519922771723264.jpg"
    this.setUserInfoStorage(this.data.userInfo)
    this.redirectToIndexPage()

  },
  
  //微信登录
  bindgetuserinfo(e) {
    //用户拒绝用微信登录
    if (!e.detail.userInfo) return;
    //同意用微信，获取微信的用户信息赋予全局userInfo
    this.data.userInfo = e.detail.userInfo;
    this.setUserInfoStorage(this.data.userInfo)
    this.redirectToIndexPage();
  },

  //用户第一次登录成功后信息写入缓存
  setUserInfoStorage(userInfo) {
    wx.setStorageSync('userInfo', userInfo)
  },

  //跳转到首页
  redirectToIndexPage() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  }

})